package com.bluejie.bluej.java.lock;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by bluej on 2018/1/28.
 */
public class LockDemo {
    private static Object obj = new Object();
    private static Lock lock = new ReentrantLock();
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getId()+"----"+Thread.currentThread().getName());

        Runnable m1 = new LockMod(obj,lock);
        Runnable m2 = new LockMod(obj,lock);

        new Thread(m1,"线程m1").start();
        new Thread(m2,"线程m2").start();


        System.out.println("over");
    }
}
class LockMod implements Runnable{
    private Object obj ;
    private Lock lock;
    public LockMod(Object obj,Lock lock){
        this.obj = obj;
        this.lock = lock;
    }
    @Override
    public void run() {
        //syn();
        lock();
    }

    private void syn(){
        synchronized (obj){
            handle();
        }
    }

    private void lock(){
        lock.lock();
        try {
            handle();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }
    }

    private void handle(){
        System.out.println(Thread.currentThread().getId()+"----"+Thread.currentThread().getName());
        while (true){
            int a = 0;
        }
    }
}